<?php
/**
 * Created by PhpStorm.
 * User: bob
 * Date: 2021/10/2
 * Time: 9:14 AM
 */

namespace App\Api\Web\Services;
use App\Api\Admin\Models\Community;
use App\Api\Auth\Models\UsersOpenId;
use App\Api\Web\Models\Bbs;
use App\Api\Web\Models\HouseWanted;
use App\Libs\Wechat\WechatService;

class BbsService
{
    public static function release($params, $from, $userId=0)
    {
        $id = data_get($params, 'id', '');
        $title = data_get($params, 'title', '');
        $text = data_get($params, 'text', '');
        $status = data_get($params, 'status', '');
        $main_img = data_get($params, 'main_img', '');
        if (empty($id)) {
            $status = 100;
            if ($from == 'admin') {
                $status = '200';
            } else {
                $appId = config('payment.jueqiweilaixcx.wechat.app_id');
                $secret = '';
                if ($appId == config('payment.jueqiweilaixcx.wechat.app_id')) {
                    $secret = config('payment.jueqiweilaixcx.wechat.app_secret');
                }
                $wechat  = new WechatService($appId, $secret);
                $r = $wechat->msgSecCheck($text);
                $r = $wechat->msgSecCheck($title);

            }
            $r = Bbs::create([
                'title' => $title,
                'main_img' => $main_img,
                'user_id' => $userId,
                'text' => $text,
                'status' => '200',
            ]);
        } else {
            $updates = [];
            empty($title) ? : $updates['title'] = $title;
            empty($text) ? : $updates['text'] = $text;
            empty($status) ? : $updates['status'] = $status;
            empty($main_img) ? : $updates['main_img'] = $main_img;
            if (!empty($updates)) {
                Bbs::find($id)
                    ->update($updates);
            }
            $r = 1;
        }

        return $r;
    }
}